package com.rico.deploy.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.rico.common.response.MessageUtils;
import com.rico.common.response.RestResponse;
import com.rico.deploy.entity.Academic;
import com.rico.deploy.entity.Classroom;
import com.rico.deploy.entity.ClassroomType;
import com.rico.deploy.service.AcademicService;
import com.rico.deploy.service.ClassroomService;
import com.rico.deploy.service.ClassroomTypeService;
import com.rico.deploy.vo.ClassroomVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @author 夏智力
 * @FileName ClassroomController
 * @date 2021/5/6 17:38
 * @Software: IntelliJ IDEA
 */
@Controller
@Slf4j
public class ClassroomController {

    @Autowired
    private ClassroomService classroomService;
    @Autowired
    private AcademicService academicService;
    @Autowired
    private ClassroomTypeService classroomTypeService;

    /**
     * list列表
     * @return
     */
    @GetMapping("classroom/list")
    @ResponseBody
    public RestResponse list(
            Integer page,
            Integer limit,
            @RequestParam(value="keyword",defaultValue = "教育大楼") String keyword){

        return MessageUtils.success(classroomService.selectAll(page, limit, keyword), (int)classroomService.selectCount(keyword));
    }



    @PostMapping("classroom/post")
    @ResponseBody
    public RestResponse post(Classroom classroom){
        log.info(classroom.toString());
        int i = classroomService.insertClassroom(classroom);
        if ( i == 1 ){
            return MessageUtils.success("添加成功");
        }else {
            return MessageUtils.fail("添加失败");
        }
    }

    @PostMapping("classroom/put")
    @ResponseBody
    public RestResponse put(Classroom classroom){
        log.info(classroom.toString());
        int i = classroomService.updateClassroom(classroom);
        if ( i == 1 ){
            return MessageUtils.success(null);
        }else {
            return MessageUtils.fail(null);
        }
    }

    @GetMapping("classroom/delete")
    @ResponseBody
    public RestResponse delete(Integer id){
        log.info("{}", id);
        int i = classroomService.deleteClassroomById(id);
        if ( i == 1 ){
            return MessageUtils.success("删除成功");
        }else {
            return MessageUtils.fail("删除失败");
        }
    }

    /**
     * 教学楼左边树
     * @return
     */
    @GetMapping("classroom/leftMenu")
    @ResponseBody
    public RestResponse LeftTreeView(){
        return MessageUtils.success("操作成功",classroomService.LeftTreeView());
    }


    /**
     * 添加页面
     * @param
     * @return
     */
    @GetMapping("classroom/add")
    public String htmladd(Model model){
        List<ClassroomType> classroomTypes = classroomTypeService.selectAll();
        model.addAttribute("classroomTypes",classroomTypes);
        return "page/manage/classroomManage/add";
    }

    /**
     * 编辑页面
     * @param id
     * @return
     */
    @GetMapping("classroom/edit")
    public String htmledit(Integer id, Model model){
        Classroom classroom = classroomService.selectById(id);
        ClassroomVO classroomVO = new ClassroomVO();
        // 赋值
        BeanUtils.copyProperties(classroom,classroomVO);
        List<Academic> academics = academicService.selectAll();
        List<ClassroomType> classroomTypes = classroomTypeService.selectAll();
        // 学院赋值
        for(Academic academic : academics ){
            if ( academic.getAcademic_code().equals(classroom.getAcademicCode())){
                classroomVO.setAcademicName(academic.getAcademic_name());
                classroomVO.setCampusCode(academic.getAcademic_code());
                classroomVO.setCampusName(academic.getAcademic_name());
                break;
            }
        }
        // 班级赋值
        for (ClassroomType classroomType : classroomTypes ){
            if (classroomType.getId().equals(classroom.getClassroomTypeId())){
                classroomVO.setTypeName(classroomType.getTypeName());
                break;
            }
        }
        model.addAttribute("classroomTypes",classroomTypes);
        model.addAttribute("academics",academics);
        model.addAttribute("classroomVO",classroomVO);
        return "page/manage/classroomManage/edit";
    }


    /**
     * 返回视图
     * @return
     */
    @GetMapping("classroomManage")
    public String html(){
        return "page/manage/classroomManage/classroomManage";
    }
}
